package com.sucisoft.base.network.report;

import android.app.Application;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheEntity;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.callback.Callback;
import com.lzy.okgo.interceptor.HttpLoggingInterceptor;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.request.PostRequest;
import com.sucisoft.base.network.callback.ICallback;
import com.sucisoft.base.network.config.NetC;
import com.sucisoft.base.network.interfaces.IHttpBase;
import com.sucisoft.base.tools.AesUtils;

import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;

import okhttp3.OkHttpClient;

/**
 * 2033152950
 * Created by zf on 2017/8/19 0019.
 */

public class OkGORequest implements IHttpBase {

    private static OkHttpClient okHttpClient = null;
    private static OKInit okInit = null;

    public OkGORequest(Application application) {
        if (okInit == null) {
            okInit = new OKInit(application);
        }
    }

    @Override
    public void PostCall(NetC config, ICallback callback) {
        final Gson gson = new Gson();
        String reqJson = gson.toJson(config.getMap(), Map.class);
        String value = AesUtils.encrypt(reqJson, "12345678abcdefgh", "12345678abcdefgh");
        HttpParams params = new HttpParams();
        params.put("aesEntity", value);
//        for (String a : config.getMap().keySet()) {
//            params.put(a, String.valueOf(config.getMap().get(a)));
//        }
        PostRequest<String> postRequest = OkGo.<String>post(config.getUrl()).tag(config.getTag()).params(params);
        if (config.IsFile()) {
            postRequest.addFileParams(config.getFileKey(), config.getFile());
        }
        postRequest.execute((Callback<String>) callback);
    }


    @Override
    public void GetCall(NetC config, ICallback callback) {
        HttpParams params = new HttpParams();
        for (String a : config.getMap().keySet()) {
            params.put(a, String.valueOf(config.getMap().get(a)));
        }
        OkGo.<String>get(config.getUrl()).tag(config.getTag()).params(params).execute((Callback<String>) callback);
    }

    @Override
    public void cancel(Object tag) {
        OkGo.cancelTag(okHttpClient, tag);
    }

    public static class OKInit {
        public OKInit(Application application) {
            OkHttpClient.Builder builder = new OkHttpClient.Builder();
            HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor("--OkGo--");
            //log打印级别，决定了log显示的详细程度
            loggingInterceptor.setPrintLevel(HttpLoggingInterceptor.Level.BODY);
            //log颜色级别，决定了log在控制台显示的颜色
            loggingInterceptor.setColorLevel(Level.INFO);
            builder.addInterceptor(loggingInterceptor);
            //全局的读取超时时间
            builder.readTimeout(5000, TimeUnit.MILLISECONDS);
            //全局的写入超时时间
            builder.writeTimeout(5000, TimeUnit.MILLISECONDS);
            //全局的连接超时时间
            builder.connectTimeout(5000, TimeUnit.MILLISECONDS);
            okHttpClient = builder.build();
            OkGo.getInstance().init(application)
                    .setOkHttpClient(okHttpClient)
                    .setCacheMode(CacheMode.NO_CACHE)
                    .setCacheTime(CacheEntity.CACHE_NEVER_EXPIRE)
                    .setRetryCount(3);
        }
    }
}
